/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.ecms.webSetting.service.impl;

import com.ecms.webSetting.domain.VideoSetting;
import com.ecms.webSetting.repository.WebRelateConfigRepository;
import me.zhengjie.utils.*;
import lombok.RequiredArgsConstructor;
import com.ecms.webSetting.repository.VideoSettingRepository;
import com.ecms.webSetting.service.VideoSettingService;
import com.ecms.webSetting.service.dto.VideoSettingDto;
import com.ecms.webSetting.service.dto.VideoSettingQueryCriteria;
import com.ecms.webSetting.service.mapstruct.VideoSettingMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author ecms
* @date 2021-12-13
**/
@Service
@RequiredArgsConstructor
public class VideoSettingServiceImpl implements VideoSettingService {
    private final WebRelateConfigRepository webRelateConfigRepository;
    private final VideoSettingRepository videoSettingRepository;
    private final VideoSettingMapper videoSettingMapper;

    @Override
    public Map<String,Object> queryAll(VideoSettingQueryCriteria criteria, Pageable pageable){
        Page<VideoSetting> page = videoSettingRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(videoSettingMapper::toDto));
    }

    @Override
    public List<VideoSettingDto> queryAll(VideoSettingQueryCriteria criteria){
        return videoSettingMapper.toDto(videoSettingRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public VideoSettingDto findById(Long id) {
        VideoSetting videoSetting = videoSettingRepository.findById(id).orElseGet(VideoSetting::new);
        ValidationUtil.isNull(videoSetting.getId(),"VideoSetting","id",id);
        return videoSettingMapper.toDto(videoSetting);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public VideoSettingDto create(VideoSetting resources) {
        resources.setCreateBy(SecurityUtils.getCurrentUsername());
        resources.setUpdateBy(SecurityUtils.getCurrentUsername());
        return videoSettingMapper.toDto(videoSettingRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(VideoSetting resources) {
        resources.setUpdateBy(SecurityUtils.getCurrentUsername());
        VideoSetting videoSetting = videoSettingRepository.findById(resources.getId()).orElseGet(VideoSetting::new);
        ValidationUtil.isNull( videoSetting.getId(),"VideoSetting","id",resources.getId());
        videoSetting.copy(resources);
        videoSettingRepository.save(videoSetting);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            videoSettingRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<VideoSettingDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (VideoSettingDto videoSetting : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("标题", videoSetting.getTitle());
            map.put("内容", videoSetting.getContext());
            map.put("英文内容", videoSetting.getEnContext());
            map.put("外链", videoSetting.getHrefLink());
            map.put("视频路径", videoSetting.getVideoUrl());
            map.put("创建者", videoSetting.getCreateBy());
            map.put("更新者", videoSetting.getUpdateBy());
            map.put("创建日期", videoSetting.getCreateTime());
            map.put("更新时间", videoSetting.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}